<?php
namespace app\common\model;

use think\Model;
use think\Db;
use think\Config;
use think\Loader;
class Subject extends Model
{
    // 指定表名,不含前缀
    protected $name = 'subject';

    /**
     *  根据id查询科目配置 并返回题型数组 仅做为前端查询
     * @param $id
     * @return array|bool
     */
   public function getSettingBySubjectId($id){
       $arr=$this->field('s_setting')->where('isdelete',0)
           ->where('id',$id)
           ->find();
       if ($arr){
          $setting =unserialize($arr['s_setting']);
          $questypes = $setting['questypes'];
//          $questypes = array_keys($questypes) ;
       }else{
           return false;
       }
       return $questypes;
   }

    /**
     * 获得科目
     */
    public function subject($id=0){
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        if($id){
            $arr=Db::name('subject')->field('id,s_name,s_setting')->where('isdelete',0)
                ->where('id',$id)
//                ->where('admin_reg',$_SESSION['think']['reg'])
                ->find();
            return $arr;
        }else{
            $arr=Db::name('subject')->field('id,s_name')->where('isdelete',0)->where(['admin_reg'=>['in',$child_plaform]])->select();
            return $arr;
        }
    }
   
    /**
     * 获得科目已绑定的题型
     */
    public function getSubjectQuestType($id=0){
		if(empty($id)) return '';
        $child_plaform = getChildAdminRegByPlaformId($this->plaform);
        $plaform_parentid = Db::name('plaform')->field('parentid')->where(['admin_reg'=>$_SESSION["think"]["reg"]])->find();
		if($plaform_parentid['parentid']){
			
			$upadmin = Db::name('plaform')->field('admin_reg')->where(['id'=>$plaform_parentid['parentid']])->find();
			$child_plaform[] = $upadmin['admin_reg'];
		}
		
		$arr=Db::name('subject')->field('id,s_name,s_setting')->where('isdelete',0)->where('id',$id)->where(['admin_reg'=>['in',$child_plaform]])->find();
		
		
        
		if(!empty($arr['s_setting'])){
			$s_setting = unserialize($arr['s_setting']);
			$s_setting = array_keys($s_setting['questypes']);
		}
		return $s_setting;        
    }


    /**
     * 查询和科目相关联信息
     * @param int $id
     * @param string $field
     * @param null $join
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    function getListById($id=0,$field = '*',$join = null){
        if(empty($id)) return '';
        if(!empty($join)){
            $table = $join[0];
            $condition = $join[1];
            $arr=Db::name('subject')->alias('a')
                ->field($field)
                ->join($table,$condition,'left')
                ->where('a.id',$id)
                ->select();
        }else{
            $arr=Db::name('subject')->alias('a')
                ->field($field)
                ->where('a.id',$id)
                ->select();
        }
        return $arr;
    }
	
	 /**
     * 获得科目 arr
     */
    public function subjectAll($q_subjectid=0){
			
			$id = array();
			foreach($q_subjectid as $v){
				$id[] = $v['q_subjectid'];
			}
            $arr=Db::name('subject')->field('id,s_name')->where('isdelete',0)
                ->where(['id'=>['in',$id]])
                ->select();
            return $arr;
        
    }
}
